<?php
class LZ_Event
{
	var $id = 'event_id';
	var $name = 'name';
	var $description = 'description';
	var $from_time = 'from_time';
	var $to_time = 'to_time';
	var $location = 'location';
	var $host = 'host';
	
	var $table = 'event';
	
	function LZ_Event()
	{
		$this->table = LZ_MYSQL_PREFIX.$this->table;
	}
	
	function add( $data = array())
	{
		global $db;
		$sql = make_insert_sql($this->table,$data);
		return ($db->query($sql));
	}
	
	function delete($event_id)
	{
		global $db;
		$sql = "DELETE FROM $this->table WHERE $this->id = '$event_id';";
		return $db->query($sql);
	}
	
	function update($event_id,$data)
	{
		global $db;
		$sql = make_update_sql($this->table,$data, array('event_id' => $event_id));
		return $db->query($sql);
	}
	
	function get_one($event_id)
	{
		global $db;
		$sql = "SELECT * FROM `{$this->table}` WHERE `{$this->id}` = '$event_id';";
		return $db->get_one($sql);	
	}
	
	function get_list($data=array())
	{
		global $db;
		$_s = $data['select']?$data['select']:'*';
		$sql = "SELECT $_s FROM `{$this->table}` WHERE 1=1 ";
		if ($data['search'])
		{
			$sql.=" AND (`$this->name` LIKE '%".$data['search']."%' OR `$this->description` LIKE '%".$data['search']."%' )";
		}
		
		if ($data['from_time'])
			$sql.= " AND `to_time` >= ".$data['from_time']." ";
		if ($data['to_time'])
			$sql.= " AND `from_time` <=".$data['to_time']." ";
		
		if (!$data['order'])
			$sql.= " ORDER BY `from_time` DESC ";		
		else
			$sql.= " ORDER BY `".$data['order'].'`';
		
		if ($data['from'] && $data['total'])
		{
			$sql.=" LIMIT ".$data['from'].",".$data['total'];
		}
		else if ($data['total'])
		{
			$sql.=" LIMIT ".$data['total'];
		}
		$sql.= ';';
		return $db->get_all($sql);
	}
}
?>